using System.Data;
using PCSComProduct.Items.DS;
using PCSComProduction.DCP.DS;
using PCSComProduction.WorkOrder.DS;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;


namespace PCSComProduction.WorkOrder.BO
{
	public class ConvertWorkorderBO //:  IConvertWorkorderBO
	{
		// Methods
		public void Add(object pObjectDetail)
		{
		}

		//[AutoComplete(true)]
		public void ConvertWorkOrder(object pobjWOMaster, DataSet pdstDetail,params string[] strFGoodsCode)
		{
			PRO_WorkOrderMasterDS dsMaster = new PRO_WorkOrderMasterDS();
			UtilsBO boUtils = new UtilsBO();
			PRO_WorkOrderMasterVO voMaster = (PRO_WorkOrderMasterVO)pobjWOMaster;
			voMaster.WorkOrderNo = boUtils.GetNoByMask("PRO_WorkOrderMaster", "WorkOrderNo", voMaster.TransDate, "YYYYMMDD0000");
			// Get Productionline
			PRO_ProductionLineVO voProductionLine = (PRO_ProductionLineVO) new PRO_ProductionLineDS().GetObjectVO(voMaster.ProductionLineID); 
			string strWONo = voProductionLine.Code;
			if (strFGoodsCode.Length >0)
			{
				strWONo = strFGoodsCode[0].ToString() + strWONo;
			}
			voMaster.WorkOrderNo = strWONo + voMaster.WorkOrderNo;
			voMaster.WorkOrderMasterID = dsMaster.AddAndReturnID(voMaster);
			DataSet dstData = pdstDetail.Clone();
			string arrItemList = "0";
			int intLine = 1;
			foreach (DataRow drowItem in pdstDetail.Tables[0].Rows)
			{
				DataRow drowNew = dstData.Tables[0].NewRow();
				foreach (DataColumn dcolNew in dstData.Tables[0].Columns)
					drowNew[dcolNew.ColumnName] = drowItem[dcolNew.ColumnName];
				drowNew["WorkOrderMasterID"] = voMaster.WorkOrderMasterID;
				drowNew["Line"] = intLine;
				// get list of item to copy bom
				arrItemList += "," + drowItem[PRO_WorkOrderDetailTable.PRODUCTID_FLD].ToString();
				// add to detail table
				dstData.Tables[0].Rows.Add(drowNew);
				intLine++;
			}
			PRO_WorkOrderDetailDS dsWODetail = new PRO_WorkOrderDetailDS();
			dsWODetail.UpdateDataSet(dstData);

			// get the list of new work orderdetail
			DataSet dstDetail = dsWODetail.List(voMaster.WorkOrderMasterID);
			// get bom from item bom
			ITM_BOMDS dsItemBOM = new ITM_BOMDS();
			ITM_RoutingDS dsRouting = new ITM_RoutingDS();
			DataSet dstItemBOM = dsItemBOM.List(arrItemList);
			DataSet dstItemRouting = dsRouting.ListRoutingByProduct(arrItemList);
			// work order bom schema
			PRO_WOBOMDS dsWOBOM = new PRO_WOBOMDS();
			DataSet dstWOBOM = dsWOBOM.List(0);
			// work order routing schema
			DataSet dstWORouting = dstItemRouting.Clone();
			dstWORouting.Tables[0].TableName = PRO_WORoutingTable.TABLE_NAME;
			dstWORouting.Tables[0].Columns.Add(PRO_WORoutingTable.WORKORDERMASTERID_FLD, typeof(int));
			dstWORouting.Tables[0].Columns.Add(PRO_WORoutingTable.WORKORDERDETAILID_FLD, typeof(int));
			// copy bom and routing for each detail item
			foreach (DataRow drowDetail in dstDetail.Tables[0].Rows)
			{
				string strWOMasterID = drowDetail[PRO_WorkOrderDetailTable.WORKORDERMASTERID_FLD].ToString();
				string strWODetailID = drowDetail[PRO_WorkOrderDetailTable.WORKORDERDETAILID_FLD].ToString();
				string strProductID = drowDetail[PRO_WorkOrderDetailTable.PRODUCTID_FLD].ToString();

				#region list of components from item bom

				DataRow[] drowComponents = dstItemBOM.Tables[0].Select(ITM_BOMTable.PRODUCTID_FLD + "=" + strProductID, ITM_BOMTable.COMPONENTID_FLD + " ASC");
				if (drowComponents.Length > 0)
				{
					// new work order bom
					foreach (DataRow drowChild in drowComponents)
					{
						DataRow drowNewBom = dstWOBOM.Tables[0].NewRow();
						foreach (DataColumn dcolChild in dstItemBOM.Tables[0].Columns)
							drowNewBom[dcolChild.ColumnName] = drowChild[dcolChild.ColumnName];
						drowNewBom[PRO_WOBOMTable.WORKORDERMASTERID_FLD] = strWOMasterID;
						drowNewBom[PRO_WOBOMTable.WORKORDERDETAILID_FLD] = strWODetailID;
						drowNewBom[PRO_WOBOMTable.PRODUCTID_FLD] = strProductID;
						dstWOBOM.Tables[0].Rows.Add(drowNewBom);
					}
				}

				#endregion

				#region routing
				DataRow[] drowRoutings = dstItemRouting.Tables[0].Select(ITM_RoutingTable.PRODUCTID_FLD + "=" + strProductID);
				if (drowRoutings.Length > 0)
				{
					int step = 0;
					foreach (DataRow drowChild in drowRoutings)
					{
						// new work order routing
						DataRow drowNewRouting = dstWORouting.Tables[0].NewRow();
						// increase step
						step++;
						foreach (DataColumn dcolChild in dstItemRouting.Tables[0].Columns)
							drowNewRouting[dcolChild.ColumnName] = drowChild[dcolChild.ColumnName];
						drowNewRouting[PRO_WORoutingTable.WORKORDERMASTERID_FLD] = strWOMasterID;
						drowNewRouting[PRO_WORoutingTable.WORKORDERDETAILID_FLD] = strWODetailID;
						drowNewRouting[PRO_WORoutingTable.PRODUCTID_FLD] = strProductID;
						drowNewRouting[PRO_WORoutingTable.STEP_FLD] = step;
						dstWORouting.Tables[0].Rows.Add(drowNewRouting);
					}
				}
				#endregion
			}
			// save new bom to database
			dsWOBOM.UpdateDataSet(dstWOBOM);
			// save new routing to datbase
			PRO_WORoutingDS dsWORouting = new PRO_WORoutingDS();
			dsWORouting.UpdateDataSet(dstWORouting);
		}

		public void Delete(object pObjectVO)
		{
		}

		//[AutoComplete(true)]
		public DataTable GetBOM(bool blnCTTC)
		{
			ITM_BOMDS dsBOM = new ITM_BOMDS();
			return dsBOM.GetBomToConvert(blnCTTC);
		}

		public object GetObjectVO(int pintID, string VOclass)
		{
			return null;
		}

		//[AutoComplete(true)]
		public DataSet GetWorkOrderDetail(int pintMasterID)
		{
			PRO_WorkOrderDetailDS dsDetail = new PRO_WorkOrderDetailDS();
			return dsDetail.List(pintMasterID);
		}

		//[AutoComplete(true)]
		public DataTable ListItem()
		{
			ITM_ProductDS dsProduct = new ITM_ProductDS();
			return dsProduct.List().Tables[0];
		}

		public void Update(object pObjectDetail)
		{
		}

		public void UpdateDataSet(DataSet dstData)
		{
		}
		//[AutoComplete(true)]
		public bool IsConverted(int pintSaleOrderDetailID)
		{
			PRO_WorkOrderDetailDS dsDetail = new PRO_WorkOrderDetailDS();
			return dsDetail.IsConverted(pintSaleOrderDetailID);
		}
	}
}
